﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spore.Validator;
using QunYing.Common;
using System.ServiceModel;


namespace Client
{
    public partial class VipLook : BasePopForm
    {
       List<QunYing.Model.VipServices> services;
       List<QunYing.Model.VipRank> vipRank;
       QunYing.Model.VipInfo m_vipInfo;
       
        public VipLook()
        {
            InitializeComponent();
            this.services = (new QunYing.BLL.VipServices()).GetModelList(string.Empty);
            this.vipRank = (new QunYing.BLL.VipRank()).GetModelList(string.Empty);
            this.cbe_会员级别.SelectedIndexChanged += new EventHandler(cbe_会员级别_SelectedIndexChanged);
            this.txt_经销商.Text = GlobalInfo.UserLoginInfo.用户ID;
            this.dte_入会时间.DateTime = DateTime.Now;
            this.FormValidator.SetControlValitors(txt_会员ID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_省份, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_水族箱编号, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.cbe_会员级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_VipCardID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_固定电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_移动电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_通信地址, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_城市, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_email, ValidatorDefaults.Email);
            this.FormValidator.SetControlValitors(this.txt_name, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.dte_入会时间, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.dte_到期时间, ValidatorDefaults.NotNull);
        }

        public void BindVipInfo(QunYing.Model.VipInfo vipInfo)
        {
            this.m_vipInfo = vipInfo;
            var opera= ClientProxyFactory.GetProxy<IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
            Bitmap[] bp=opera.GetImages(vipInfo.经销商编号, new string[] {m_vipInfo.会员卡号+".jpg" });
            (opera as ICommunicationObject).Close();
            if (bp.Length != 0)
            {
                this.pet_地址图片.Image = bp[0];
            }
            this.txt_email.Text = m_vipInfo.电邮地址;
            this.txt_name.Text = m_vipInfo.用户姓名;
            this.txt_VipCardID.Text = m_vipInfo.会员卡号;
            this.txt_城市.Text = m_vipInfo.所在城市;
            this.txt_固定电话.Text = m_vipInfo.固定电话;
            this.txt_会员ID.Text = m_vipInfo.用户ID;
            this.txt_水族箱编号.Text = m_vipInfo.水族箱设备ID;
            this.txt_通信地址.Text = m_vipInfo.通信地址;
            this.txt_移动电话.Text = m_vipInfo.移动电话;
            this.txt_省份.Text = m_vipInfo.所在省份;
            this.cbe_会员级别.SelectedIndex = this.cbe_会员级别.Properties.Items.IndexOf(m_vipInfo.会员级别);
            this.dte_到期时间.DateTime = m_vipInfo.到期时间;
            this.dte_入会时间.DateTime = m_vipInfo.入会时间;
            this.txt_其他信息.Text = m_vipInfo.其他信息;
            if (m_vipInfo.所选服务项 == null) return;
            string[] strs = this.m_vipInfo.所选服务项.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
            foreach (var vv in strs)
            {
                QunYing.Model.VipServices vs = this.services.FirstOrDefault((ee) => ee.ID.ToString() == vv);
               this.txt_服务项.SetItemChecked(this.txt_服务项.Items.IndexOf(vs),true);
            }

        }

        void cbe_会员级别_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_服务项.BeginUpdate();
            this.txt_服务项.Items.Clear();
            string[] strs = vipRank.First((v) => { return v.等级名称 == cbe_会员级别.Text; }).基本服务条款.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
             foreach(var v in strs)
             {
                 var temp = services.First((vv) => { return vv.ID.ToString() == v; });
                 this.txt_服务项.Items.Add(temp, false);
             }
            this.txt_服务项.EndUpdate();
        }

        protected override void OnLoad(EventArgs e)
        {
           
            base.OnLoad(e);
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            //this.Close();
           // if (!this.FormValidator.ValidAll()) return;
           //string[] devices = this.txt_水族箱编号.Text.Trim().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
           //QunYing.BLL.ICSA_Device ser = new QunYing.BLL.ICSA_Device();
           //foreach (var vv in devices)
           //{
           //    if (ser.Exists(vv))
           //    {
           //        GlobalInfo.MsgBoxShowError("不存在设备：" + vv);
           //        return;
           //    }
           //}
           //m_vipInfo.到期时间 = this.dte_到期时间.DateTime;
           //m_vipInfo.入会时间 = this.dte_入会时间.DateTime;
           //m_vipInfo.电邮地址 = this.txt_email.Text.Trim();
           //m_vipInfo.固定电话 = this.txt_固定电话.Text.Trim();
           //m_vipInfo.会员级别 = this.cbe_会员级别.Text;
           // m_vipInfo.会员卡号 = this.txt_VipCardID.Text;
           //m_vipInfo.经销商编号 = this.txt_经销商.Text;
           //m_vipInfo.水族箱设备ID = this.txt_水族箱编号.Text.Trim();
           //string services = string.Empty;
           //foreach (var vv in this.txt_服务项.CheckedItems)
           //{
           //    services+=  this.services.FirstOrDefault((ee) => ee.服务项 == vv.ToString()).ID.ToString()+",";
           //}
           //services = services.TrimEnd(',');
           //m_vipInfo.所选服务项 = services;
           //m_vipInfo.所在城市 = this.txt_城市.Text;
           //m_vipInfo.所在省份 = this.txt_省份.Text;
           //m_vipInfo.通信地址 = this.txt_通信地址.Text;
           //m_vipInfo.移动电话 = this.txt_移动电话.Text;
           //m_vipInfo.用户ID = this.txt_会员ID.Text;
           //m_vipInfo.用户姓名 = this.txt_name.Text;
           //GlobalInfo.MsgBoxShow("会员申请已提交，请等待审核。","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
           this.Close();
        }
    }
}
